package cn.xfdzx.android.apps.shop.adapter;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.tools.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.GoodsLiblaryListBean;
import cn.xfdzx.android.apps.shop.bean.OrderDetailInfoBeen;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;

/**
 * Creator: zf.
 * Date: 2020/8/1.
 * Description:商品库.
 */
public class ShopInGoodslibrarListAdapter extends BaseQuickAdapter<GoodsLiblaryListBean.DataBean, BaseViewHolder>  {
    private OnItemOnClickListener onItemOnClickListener;
    private List<GoodsLiblaryListBean.DataBean> dataBeans = new ArrayList<>();//商品列表
    public ShopInGoodslibrarListAdapter(@Nullable List<GoodsLiblaryListBean.DataBean> data) {
        super(R.layout.item_shopin_goodlibrary_list, data);
    }

    @Override
    public void setData(int index, @NonNull GoodsLiblaryListBean.DataBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final GoodsLiblaryListBean.DataBean date) {
       //ic_serviceyesyellow d99999_oval
        CheckBox check_item = viewHolder.getView(R.id.check_item);
        ImageView iv_good_image = viewHolder.getView(R.id.iv_good_image);
        TextView tv_added = viewHolder.getView(R.id.tv_added);
//        TextView tv_good_name = viewHolder.getView(R.id.tv_good_name);
//        TextView tv_good_old_price = viewHolder.getView(R.id.tv_good_old_price);

        viewHolder.setText(R.id.tv_good_name,date.getTitle())
        .setText(R.id.tv_good_old_price,"¥"+date.getMarketprice());
        check_item.setChecked(date.isChecked());
        // 1 => 已经导入过  2 => 未导入过
        if (date.getIs_check() == 1){
            tv_added.setVisibility(View.VISIBLE);
        }else {
            tv_added.setVisibility(View.GONE);
        }

        ImageGlideUtil.loadImage(mContext,iv_good_image,date.getThumb());

        check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) { //判断view是否是用户点击
                    return;
                }
                check_item.setChecked(isChecked);
                mData.get(viewHolder.getLayoutPosition()).setChecked(isChecked);
                onItemOnClickListener.onItemCheckClick(viewHolder.getLayoutPosition(),mData);

            }
        });

    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<GoodsLiblaryListBean.DataBean> list){
        this.dataBeans = list;
        super.replaceData(list);
    }

    public void addDatas(List<GoodsLiblaryListBean.DataBean> list){
        super.addData(list);
    }

    public void setItemCheckListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemCheckClick(int position, List<GoodsLiblaryListBean.DataBean> dataBeans);
    }

}
